package cloud.xlh.my_system.service.impl;

import cloud.xlh.my_system.common.response.Response;
import cloud.xlh.my_system.mapper.GenTableColumnMapper;
import cloud.xlh.my_system.mapper.GenTableMapper;
import cloud.xlh.my_system.pojo.*;
import cloud.xlh.my_system.service.DatabaseService;
import cloud.xlh.my_system.service.GenTableService;

import cloud.xlh.my_system.utils.Convert;
import cloud.xlh.my_system.utils.GenUtils;
import cloud.xlh.my_system.utils.SequenceUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : code-generator
 * @ Author: XuLeHuang
 * @ Date: 2022/6/20 15:36
 * @ Description:
 */
@Service
@Transactional
public class GenTableServiceImpl implements GenTableService {
    @Autowired
    @Lazy
    private DatabaseService databaseService;
    @Autowired
    private GenTableMapper genTableMapper;
    @Autowired
    private GenTableColumnMapper genTableColumnMapper;
    @Override
    public Response<Map> QueryGenTableByCondition(DatabaseRequest databaseRequest) {
        int limit = (int)databaseRequest.getLimit();
        int page = (int)databaseRequest.getPage();
        Long databaseId = Long.parseLong(databaseRequest.getDatabaseId());
        List<GenTable> genTableList= genTableMapper.findAllTableByDatabaseIdToPage(databaseId , limit ,page - 1);
        //List<Long> dbIds = projectList.stream().map(Project::getDatabaseId).collect(Collectors.toList());
        HashMap<String, Object> mapRes = new HashMap<>();
        mapRes.put("rows" , genTableList);
        mapRes.put("limit" , limit);
        mapRes.put("total" , page);
        return Response.success(mapRes);
    }

    @Override
    public Response<String> importTable(String dbName , String databaseId) {
        List<GenTable> tableList = findAllTableByDatabaseName(dbName);
       /* List<String> tableNames = new ArrayList<>();
        genTableList.stream().forEach(genTable -> tableNames.add(genTable.getTableName()));
        String[] tableNames = Convert.toStrArray(tables);
        List<GenTable> tableList = genTableMapper.selectDbTableListByNames(tableNames);*/
        tableList.forEach(table -> {
            table.setTableId(SequenceUtil.makeStringId());
            table.setDatabaseId(databaseId);
        });
        importGenTable(tableList, "");
        return Response.success("导入" + dbName + "相关表成功");
    }

    @Override
    public List<GenTable> findAllTableByDatabaseName(String dbName) {
        return genTableMapper.findAllTableByDatabaseName(dbName);
    }

    @Override
    public List<GenTable> findAllTableByDatabaseId(String databaseId) {
        return genTableMapper.findAllTableByDatabaseId(databaseId);
    }

    @Override
    public void deleteGenTableByGenTableId(String tableId) {
        genTableMapper.deleteGenTableByGenTableId(tableId);
    }


    /**
     * 导入表结构
     *
     * @param tableList 导入表列表
     * @param operName  操作人员
     */
    @Transactional(rollbackFor = Exception.class)
    public void importGenTable(List<GenTable> tableList, String operName) {
        try {
            for (GenTable table : tableList) {
                String tableName = table.getTableName();
                GenUtils.initTable(table, operName);
                int row = genTableMapper.insertGenTable(table);
                if (row > 0) {
                    List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                    for (GenTableColumn column : genTableColumns) {
                        column.setColumnId(SequenceUtil.makeStringId());
                        GenUtils.initColumnField(column, table);
                        genTableColumnMapper.insertGenTableColumn(column);
                    }
                }
            }
        } catch (Exception e) {
            //TODO
            //throw new BusinessException("导入失败：" + e.getMessage());
        }
    }
}
